package com.grieous.wow.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.grieous.wow.biz.ArticleBiz;
import com.grieous.wow.dao.impl.ArticleDaoImpl;
import com.grieous.wow.entity.Article;
import com.grieous.wow.entity.ArticleCategory;
import com.grieous.wow.entity.PageModel;

@Service("articleBiz")
public class ArticleBizImpl implements ArticleBiz {

	@Resource
	private ArticleDaoImpl articleDao;

	@Override
	public PageModel getArticle(Integer pageSize, Integer page,
			Integer category, String orderByClause) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		if (pageSize != null) {
			page = page == null ? 1 : page;
			count = articleDao.findCountByCondition(map);
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		map.put("category", category);
		map.put("orderByClause", orderByClause);
		List<Article> list = articleDao.findListByCondition(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public boolean saveArticle(Article article) {
		if (article != null) {
			if (article.getId() != null && !article.getId().equals("")) {
				if(articleDao.update(article)==1){
					return true;
				}
			} else {
				if(articleDao.add(article)==1){
					return true;
				}
			}
		}
		return false;

	}

	@Override
	public boolean delArticle(int... ids) {
		if (ids != null && ids.length>0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if(articleDao.delete(map)==ids.length){
				return true;
			}
		}
		return false;

	}

	@Override
	public Article getArticleById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Article> list = articleDao.findListByCondition(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ArticleCategory> getIndexArticle() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", 8);
		return articleDao.getArticleByCatrgory(map);
	}
}
